package com.governtalk.backend.controller;

import com.governtalk.backend.dto.BrowseHistoryDto;
import com.governtalk.backend.service.BrowseHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/browse-history")
public class BrowseHistoryController {
    
    @Autowired
    private BrowseHistoryService browseHistoryService;
    
    @PostMapping
    public ResponseEntity<BrowseHistoryDto> addBrowseHistory(@RequestBody BrowseHistoryDto browseHistoryDto) {
        try {
            System.out.println("接收到浏览历史数据: " + browseHistoryDto);
            BrowseHistoryDto result = browseHistoryService.addBrowseHistory(browseHistoryDto);
            System.out.println("保存成功: " + result);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            System.err.println("保存浏览历史失败: " + e.getMessage());
            e.printStackTrace();
            return ResponseEntity.status(500).build();
        }
    }
    
    @GetMapping("/user/{userId}")
    public ResponseEntity<Page<BrowseHistoryDto>> getUserBrowseHistory(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<BrowseHistoryDto> result = browseHistoryService.getUserBrowseHistory(userId, page, size);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/user/{userId}/type/{contentType}")
    public ResponseEntity<Page<BrowseHistoryDto>> getUserBrowseHistoryByType(
            @PathVariable Long userId,
            @PathVariable String contentType,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<BrowseHistoryDto> result = browseHistoryService.getUserBrowseHistoryByType(userId, contentType, page, size);
        return ResponseEntity.ok(result);
    }
    
    @GetMapping("/user/{userId}/search")
    public ResponseEntity<Page<BrowseHistoryDto>> searchUserBrowseHistory(
            @PathVariable Long userId,
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<BrowseHistoryDto> result = browseHistoryService.searchUserBrowseHistory(userId, keyword, page, size);
        return ResponseEntity.ok(result);
    }
    
    @DeleteMapping("/{historyId}")
    public ResponseEntity<Void> deleteBrowseHistory(@PathVariable Long historyId) {
        browseHistoryService.deleteBrowseHistory(historyId);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/user/{userId}/batch")
    public ResponseEntity<Void> batchDeleteBrowseHistory(
            @PathVariable Long userId,
            @RequestBody Long[] historyIds) {
        browseHistoryService.batchDeleteBrowseHistory(userId, historyIds);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/user/{userId}/clear")
    public ResponseEntity<Void> clearUserBrowseHistory(@PathVariable Long userId) {
        browseHistoryService.clearUserBrowseHistory(userId);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/user/{userId}/type/{contentType}/clear")
    public ResponseEntity<Void> clearUserBrowseHistoryByType(
            @PathVariable Long userId,
            @PathVariable String contentType) {
        browseHistoryService.clearUserBrowseHistoryByType(userId, contentType);
        return ResponseEntity.ok().build();
    }
}
